home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pnl010.zip / STRLIST.PAS < prev    next >
Pascal/Delphi Source File  |  1992-03-01  |  2KB  |  66 lines

  1. Program StrList;
  2. (*Copyright (c) 1992 KHIRON Software
  3.  
  4.   All rights reserved. KHIRON Software hereby grants
  5.   permission for free distribution of this software,
  6.   and for use of this software within commercial and
  7.   non-commercial applications. This software itself
  8.   may not be distributed commercially without obtaining
  9.   written permission from KHIRON Software.
  10.  
  11.   Should you use this software in commercial product send
  12.   me a postcard at:  KHIRON Software
  13.                      P.O. Box 544
  14.                      INDOOROOPILLY Qld 4068
  15.                      AUSTRALIA
  16. *)
  17. {    This is a simple exaple of creating your own functional Collection
  18.      Object.  It derives a descendant of collection to store string
  19.      pointers that overrides the FreeItem to Dispose of a String
  20.      Pointer when the objects is disposed, and adds a new method called
  21.      Print_Sentence which prints out all the Strings in order.
  22. }
  23. Uses Objects;
  24. Type
  25.   PMyStrings = ^TMyStrings;
  26.   TMyStrings = Object(tCollection)
  27.     Constructor Init;
  28.     Procedure Print_Sentence;
  29.     procedure FreeItem(Item: Pointer); virtual;
  30.   end;
  31. Constructor TMyStrings.Init;
  32. begin
  33.   TCollection.Init(10,10);
  34. end;
  35. procedure TMyStrings.FreeItem(Item: Pointer);
  36. begin
  37.   DisposeStr(PString(Item));
  38. end;
  39. Procedure TMyStrings.Print_Sentence;
  40.   Procedure PrintWord(Item : PString); far;
  41.   begin
  42.     If Item <> nil then
  43.       Write(Item^,' ');
  44.   end;
  45. begin
  46.   ForEach(@PrintWord);   
  47.   Writeln('.');
  48. end;
  49. Var
  50.   MyStringList : pMyStrings;
  51.   Temp         : String;
  52. begin
  53.   MyStringList := New(pMyStrings,Init);
  54.   MyStringList^.Insert(NewStr('Four'));
  55.   MyStringList^.Insert(NewStr('Score'));
  56.   MyStringList^.Insert(NewStr('and'));
  57.   MyStringList^.Insert(NewStr('Twenty'));
  58.   MyStringList^.Insert(NewStr('Beers'));
  59.   MyStringList^.Insert(NewStr('Ago'));
  60.   MyStringList^.Print_Sentence;
  61.   Temp := PString(MyStringList^.At(3))^;
  62.   Writeln('The Fourth Word is ',Temp);
  63.   Dispose(MyStringList,Done);
  64. end.
  65.  
  66.